home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Math / sa / vec < prev    next >
Encoding:
Text File  |  1996-07-18  |  8.1 KB  |  263 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  3. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  4. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  5. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  6. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  7. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  8. -- Author: Matthew B. Kennel <mbk@caffeine.engr.utk.edu>
  9. -------------------------------------------------------------------
  10. partial class VEC{ET<$REAL_NUMBER{ET}} is
  11.    -- Partial becauseit requires VECTOR_LENGTH_MIXIN's to complete itself
  12.    -- We cannot subtype this generic from $VEC{ET,SAME}, though
  13.    -- instantiations of this type should be subtyped from the proper
  14.    -- abstract.  c.f. 'mat.sa'. 
  15.    -- 
  16.    -- This is a pure Sather implementation of the basic vector class.
  17.    -- "length" and dot product operations will NOT be implemented here,
  18.    -- but in auxiliary 'partial' (non-instantiable) classes that are
  19.    -- designed for only implementation inheritance. 
  20.    -- A concrete vector class of floating point numbers then would inherit
  21.    -- from this class, and the auxiliary 'length' implementation suitable for
  22.    -- it, and then subtype from the proper generics. 
  23.    -- Finally, for some element types, such as floating point numbers, there
  24.    -- will be implementations that may over-ride some of the implementaitons
  25.    -- in this class and replace them with BLAS calls. 
  26.    -- 
  27.    -- What is the difference between a VEC and an ARRAY?
  28.    -- An ARRAY is more a full-featured container class; a VEC is intended
  29.    -- as the target space of a linear operator, which might be its associated
  30.    -- matrix for instance.  Therefore ARRAY has features for sorting
  31.    -- and many copying iters, which may not all make sense in a VEC context.
  32.    -- For instance anything which changes the size of an ARRAY is not 
  33.    -- appropriate for VEC. 
  34.    
  35.    include AREF{ET};
  36.  
  37.    element_zero:ET is return ET::zero; end; 
  38.    element_one:ET is return ET::one; end; 
  39.  
  40.    dim:INT is return asize end; 
  41.    
  42.    same_size(arg:SAME):BOOL is
  43.       if void(arg) or void(self) then return false;
  44.       elsif arg.asize /= asize then return false;
  45.       else return true;
  46.       end;
  47.    end;
  48.    
  49.    is_eq(arg:SAME):BOOL pre same_size(arg) is
  50.       loop
  51.      if aelt! /= arg.aelt! then return false; end;
  52.       end;
  53.       return true; 
  54.    end;
  55.    
  56.    create(sz:INT):SAME is
  57.       res ::= new(sz); return res; 
  58.    end;
  59.    
  60.    create(arg:SAME):SAME pre ~void(arg) is
  61.       res ::= new(arg.asize); return res;
  62.    end;
  63.  
  64.    create(arg:ARRAY{ET}):SAME pre ~void(arg) is
  65.       res ::= new(arg.asize); loop res.aset!(arg.elt!); end; 
  66.       return(res);
  67.    end;
  68.    
  69.    str:STR is
  70.       -- Return a string representing the vector
  71.       -- Eg. |0,1|
  72.       res ::= #FSTR+"|";
  73.       loop res := res + aelt!(0,dim-1).str + ","; end;
  74.       res := res + [dim-1].str + "|"; 
  75.       return(res.str);
  76.    end;
  77.  
  78.    array:ARRAY{ET} pre ~void(self) is
  79.       res ::= #ARRAY{ET}(dim);
  80.       loop res.set!(aelt!) end;
  81.    end;
  82.    
  83.    copy:SAME pre ~void(self) is
  84.       res ::= create(asize);
  85.       res.acopy(self); -- should be built in fast routine
  86.       return res; 
  87.    end;
  88.    
  89.    inplace_contents(arg:SAME) pre same_size(arg) is
  90.       self.acopy(arg);
  91.    end; 
  92.    
  93.    inplace_contents_subspace(destbeg,n,srcbeg:INT,arg:SAME) pre
  94.      ~void(self) and ~void(arg) and n >= 0 and
  95.      destbeg.is_bet(0,asize-1) and
  96.      n.is_bet(0,asize-destbeg) and n <= arg.asize - srcbeg (* whew *)
  97.         is
  98.       acopy(destbeg,n,srcbeg,arg); -- should be built-in fast routine.
  99.    end;
  100.      
  101.    inplace_contents_from_function(function:ROUT{INT}:ET) pre ~void(self) is
  102.       loop idx ::= asize.times!; [idx] := function.call(idx); end; 
  103.    end;
  104.    
  105.    inplace_elements(arg:ET) pre ~void(self) is
  106.       loop aset!(arg); end; 
  107.    end;
  108.    
  109.    inplace_unit_vector(i:INT) pre ~void(self) and i.is_bet(0,asize-1) is
  110.       loop idx ::= asize.times!;
  111.      if idx = i then [i] := element_one;
  112.      else [i] := element_zero; 
  113.      end;
  114.       end;
  115.    end;
  116.  
  117.    times(s:ET):SAME pre ~void(self) is
  118.       res ::= new(asize);
  119.       loop i ::= asize.times!; res[i] := [i]*s; end;
  120.       return res;
  121.    end;
  122.    
  123.    scaled_by(s:ET):SAME is return times(s); end; 
  124.    
  125.    inplace_scaled_by(s:ET) pre ~void(self) is
  126.       loop i ::= asize.times!; [i] := [i]*s; end; 
  127.    end;
  128.    
  129.    plus(arg:SAME):SAME is 
  130.       res ::= new(asize); res.inplace_arg_plus_arg(self,arg);
  131.       return res; 
  132.    end;
  133.    
  134.    plus_arg(arg:SAME):SAME is return plus(arg); end;
  135.    
  136.    minus(arg:SAME):SAME is res ::= new(asize); 
  137.       res.inplace_arg_minus_arg(self,arg); return res 
  138.    end;
  139.    
  140.    minus_arg(arg:SAME):SAME is return minus(arg); end; 
  141.    
  142.    inplace_plus_arg(arg:SAME) pre same_size(arg) is
  143.       loop i ::= asize.times!; [i] := [i] + arg[i]; end; 
  144.    end;
  145.    
  146.    inplace_minus_arg(arg:SAME) pre same_size(arg) is
  147.       loop i ::= asize.times!; [i] := [i] - arg[i]; end; 
  148.    end;
  149.    
  150.    inplace_arg_plus_arg(arg1,arg2:SAME) pre same_size(arg1) and 
  151.      arg1.same_size(arg2) is
  152.       loop i ::= asize.times!; [i] := arg1[i] + arg2[i]; end; 
  153.    end; 
  154.    
  155.    inplace_arg_minus_arg(arg1,arg2:SAME) pre same_size(arg1) and
  156.      arg1.same_size(arg2) is
  157.       loop i ::= asize.times!; [i] := arg1[i] - arg2[i]; end; 
  158.    end; 
  159.  
  160.    plus_scaled_arg(s:ET,arg:SAME):SAME pre same_size(arg) is
  161.       res ::= new(asize); res.inplace_arg_plus_scaled_arg(self,s,arg); 
  162.       return res;
  163.    end;
  164.  
  165.    inplace_plus_scaled_arg(s:ET,arg:SAME) pre same_size(arg) is
  166.       loop i ::= asize.times!; [i] := [i] + s*arg[i]; end; 
  167.    end;
  168.  
  169.    inplace_arg_plus_scaled_arg(arg1:SAME,s:ET,arg2:SAME) pre same_size(arg1)
  170.         and arg1.same_size(arg2) is 
  171.       loop i ::= asize.times!; [i] := arg1[i] + s*arg2[i]; end; 
  172.    end;
  173.    
  174.    inplace_swapped(arg:SAME) pre same_size(arg) is
  175.       loop i ::= asize.times!;
  176.      tmp ::= [i]; [i] := arg[i]; arg[i] := tmp;
  177.       end;
  178.    end;
  179.    
  180. end;
  181. -----------------------------------------------------------------------
  182. partial class VEC_LENGTH_MIXIN{ET<$REAL_NUMBER{ET},VT<$VEC{ET,VEC}} is
  183. -- 
  184. -- NOTE most of the these definitions are NOT good for complex numbers.
  185. --
  186.    include VEC{ET};
  187.    
  188.    length_zero:ET is return ET::zero; end;
  189.    length_one:ET is return ET::one; end;
  190.  
  191.    dot(arg:SAME):ET pre same_size(arg) is 
  192.       res::= element_zero; 
  193.       loop i ::= asize.times!; res := res + [i]*arg[i]; end; 
  194.       return res;
  195.    end;
  196.    
  197.    angle_with(arg:SAME):ET is 
  198.       return cosine_angle_with(arg).acos; 
  199.    end;
  200.    
  201.    cosine_angle_with(arg:SAME):ET pre same_size(arg) is
  202.       -- cos theta = a dot b / |a|*|b|
  203.       return dot(arg) / (length * arg.length);
  204.    end;
  205.    
  206.    length_squared:ET is return dot(self); end;
  207.    
  208.    length:ET is return length_squared.sqrt; end; 
  209.    
  210.    distance_to_squared(arg:SAME):ET pre same_size(arg) is
  211.       -- we could implement in terms of other functions but I will
  212.       -- hand-write for speed. 
  213.       res ::= length_zero;
  214.       loop i ::= asize.times!; t ::= [i]-arg[i];
  215.      res := res + t*t;
  216.       end;
  217.       return res;
  218.    end;
  219.  
  220.    distance_to(arg:SAME):ET is return distance_to_squared(arg).sqrt; end;
  221.    
  222.    bounded_distance_to_squared(arg:SAME,sbnd:ET):ET
  223.       pre same_size(arg) is
  224.       -- we could implement in terms of other functions but I will
  225.       -- hand-write for speed. 
  226.       res ::= length_zero;
  227.       loop i ::= asize.times!; t ::= [i]-arg[i];
  228.      res := res + t*t;
  229.      if res > sbnd then return length_one.negate; end;
  230.       end;
  231.       return res;
  232.    end;
  233.  
  234.    inplace_normalized is inplace_scaled_by(length_one / length); end;
  235.    
  236. end;
  237. -----------------------------------------------------------------
  238. class VEC < $VEC{FLT,VEC,FLT} is
  239.    -- Concrete instantiation of a standard vector
  240.    include VEC_LENGTH_MIXIN{FLT,SAME};
  241.    const element_zero:FLT := 0.0;
  242.    const element_one:FLT := 1.0;   
  243.    const length_zero:FLT := 0.0;
  244.    const length_one:FLT := 1.0;   
  245.    
  246.    max_value: FLT is
  247.       max: FLT := FLT::minval;
  248.       loop i ::= asize.times!; 
  249.      if [i] > max then max := [i]; end;
  250.       end;
  251.       return max;
  252.    end;
  253.    
  254. end;
  255. -----------------------------------------------------------------
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.